package com.songlanyun.modules.ship.model.dto;


import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import cn.afterturn.easypoi.handler.inter.IExcelDataModel;
import cn.afterturn.easypoi.handler.inter.IExcelModel;
import com.songlanyun.common.drive.ValueObject;
import com.songlanyun.common.utils.importverify.BaseImportDto;
import com.songlanyun.modules.order.model.vo.ChildOrderSlimVO;
import com.songlanyun.modules.order.model.vo.ChildOrderVO;
import com.songlanyun.modules.order.service.ChildOrderService;
import com.songlanyun.modules.rabbitmq.util.SpringContextUtil;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.util.List;

@Data
@ExcelTarget("branchShipVO")
public class BranchShipVO extends BaseImportDto implements IExcelDataModel, IExcelModel {

    private Long id;

    @Excel(name = "订单号", orderNum = "1", width = 30D, needMerge = true)
    private String orderCode;
    /**
     * 总费用(包含运费)
     */
    @Excel(name = "费用(包含运费)", orderNum = "2", width = 30D, needMerge = true)
    private BigDecimal totalAmount;


    /**
     * 真实的物流费用
     */
    @Excel(name = "运费", orderNum = "3", width = 10D, needMerge = true)
    private BigDecimal realFreight;

    /**
     * 商品总数量
     */
    @Excel(name = "商品总数量", orderNum = "4", width = 10D, needMerge = true)
    private Integer goodsNumber;

    @Excel(name = "商品信息", orderNum = "5", width = 30D, needMerge = true)
    private String  childOrderInfo ;


    public  String getChildOrderInfo(boolean load)
    {
        if (load || childOrderInfo == null) {
            StringBuilder sb = new StringBuilder(100);
            ChildOrderService childOrderService = (ChildOrderService) SpringContextUtil.getBean("childOrderService");
            List<ChildOrderSlimVO> childOrderSlimVOS = childOrderService.vosSlimByOrderId(this.getId());
            childOrderSlimVOS.forEach(o->{
                sb.append(String.format("%s 规格:%s 数量:%d %s",o.getGoodsTitle(),o.getSkuSetName(),o.getNumber(),o.getUnit()));
                sb.append(String.valueOf((char)10));
            });
            childOrderInfo = sb.toString();
        }
        return childOrderInfo;
    }

    /**
     * 主订单下面的子订单
     */
    private List<ChildOrderSlimVO> childOrderList;

    public List<ChildOrderSlimVO> getChildOrderList(boolean load) {
        if (load || childOrderList == null) {
            ChildOrderService childOrderService = (ChildOrderService) SpringContextUtil.getBean("childOrderService");
            childOrderList = childOrderService.vosSlimByOrderId(this.getId());
        }
        return childOrderList;
    }


    @Excel(name = "快递收货人", orderNum = "6", width = 10D, needMerge = true)
    private String receiver;
    /**
     * 收货人手机号
     */

    @Excel(name = "收货人手机号", orderNum = "7", width = 10D, needMerge = true)
    private String mobile;


    /**
     * 省名称
     */
    @Excel(name = "省", orderNum = "8", width = 10D, needMerge = true)
    private String provinceName;

    /**
     * 市名称
     */
    @Excel(name = "市", orderNum = "9", width = 10D, needMerge = true)
    private String cityName;


    @Excel(name = "区", orderNum = "10", width = 10D, needMerge = true)
    private String areaName;


    /**
     * 地址
     */
    @Excel(name = "地址", orderNum = "12", width = 40D, needMerge = true)
    private String address;



    /**
     * 物流公司
     */
    @Excel(name = "物流公司名称", orderNum = "13", width = 30D, needMerge = true)
    private String expressName="";

    /**
     * 快递单号
     */
    @Excel(name = "快递单号", orderNum = "14", width = 30D, needMerge = true)
    private String logisticCode="";

    /**
     * 行号
     */
    private int rowNum;
    /**
     * 错误消息
     */
    private String errorMsg;

    @Override
    public Integer getRowNum() {
        return rowNum;
    }

    @Override
    public void setRowNum(Integer rowNum) {
        this.rowNum = rowNum;
    }

    @Override
    public String getErrorMsg() {
        return errorMsg;
    }

    @Override
    public void setErrorMsg(String errorMsg) {
        this.errorMsg  = errorMsg;
    }


    @Override
    public boolean equals(Object obj) {
        //地址相等
        if(this == obj){
            return true;
        }

        //非空性：对于任意非空引用x，x.equals(null)应该返回false。
        if(obj == null){
            return false;
        }

        if(obj instanceof BranchShipVO){
            BranchShipVO other = (BranchShipVO) obj;
            //需要比较的字段相等，则这两个对象相等
            if(this.getOrderCode().equals(other.getOrderCode()))
            {
                return true;
            }
        }

        return false;
    }

    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + (orderCode == null ? 0 : orderCode.hashCode());
        return result;
    }

}
